import { export_json_to_excel } from '@/utils/vendor/Export2Excel'
import dayjs from 'dayjs'

export function useExcelExportDownload({ columns, data, filename }) {
  const excelExportDownload = () => {
    const tHeader: any[] = []
    const filterVal: any[] = []

    columns.value?.forEach((column: any) => {
      if (
        column.export !== false &&
        column.key &&
        column.title &&
        !['index', 'selection'].includes(column.type)
      ) {
        tHeader.push(column.title)
        filterVal.push(column.key)
      }
    })
    const listData = data.value?.map((row) => {
      const displayValue = row.__displayValue__
      return filterVal?.map((key) => displayValue[key])
    })

    export_json_to_excel({
      header: tHeader,
      data: listData,
      filename: filename || dayjs(Date.now()).format('YYYYMMDD-HHmmss')
    })
  }

  return excelExportDownload
}
